<template>
  <div class="body">
    <div class="title">
      <el-form
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
        style="width: 100%"
      >
        <div class="up">
          <!-- 标签名称 -->
          <el-form-item label="标签名称">
            <el-input
              size="small"
              v-model="fromDate.tags"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <!-- 城市 -->
          <el-form-item label="城市">
            <el-select
              size="small"
              @change="changeFn"
              v-model="fromDate.province"
              placeholder="请选择"
            >
              <el-option
                v-for="(item, index) in options"
                :key="index"
                :label="item"
                :value="item"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <!-- 地区 -->
          <el-form-item label="地区">
            <el-select
              size="small"
              v-model="fromDate.city"
              placeholder="请选择"
            >
              <el-option
                v-for="(item, index) in option"
                :key="index"
                :label="item"
                :value="item"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <!-- 企业简称 -->
          <el-form-item label="企业简称">
            <el-input
              size="small"
              v-model="fromDate.shortName"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </div>
        <div class="buttom">
          <!-- 状态 -->
          <el-form-item label="状态">
            <el-select
              size="small"
              @change="changeWhat"
              v-model="fromDate.state"
              placeholder="请选择"
            >
              <el-option
                v-for="item in optionS"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <!-- 清除 -->
          <el-form-item class="clearTwo">
            <el-button @click="clear">清除</el-button>
            <el-button type="primary" @click="listFind">搜索</el-button>
          </el-form-item>
          <el-form-item style="float: right; margin-right: 0">
            <el-button type="success" icon="el-icon-edit" @click="newSubject"
              >新增学科</el-button
            >
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="alert"><i class="el-icon-info"></i> 共{{total}}条记录</div>
    <el-table
      :data="tableData"
      highlight-current-row
      style="width: 100%"
      :header-cell-style="{
        background: '##fafafa',
        color: '#909399',
        borderBottom: '2px #ccc solid',
      }"
      v-model="formInline"
    >
      <el-table-column prop="id" label="序号" />
      <el-table-column prop="number" label="企业编号" />
      <el-table-column prop="shortName" label="企业简称" />
      <el-table-column prop="tags" label="标签"> </el-table-column>
      <el-table-column prop="creatorID" label="创建者" />
      <el-table-column prop="addDate" label="创建日期" />
      <el-table-column prop="remarks" label="备注" />
      <el-table-column label="状态">
        <template slot-scope="scope">
          <span>{{ scope.row.state === 1 ? "启用" : "禁用" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template v-slot="{ row }">
          <!-- 编辑 -->
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            @click="editList(row.id)"
          ></el-button>
          <!-- 禁用 启用 -->
          <el-button
            :type="row.state === 1 ? 'warning' : 'success'"
            :icon="row.state === 1 ? 'el-icon-close' : 'el-icon-check'"
            circle
            @click="uploadList(row)"
          ></el-button>
          <!-- 删除 -->
          <el-button @click="delFn(row)" type="danger" icon="el-icon-delete" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
    <companysAdd @newDataes="newDataes" ref="dataForm" :titleInfo="titleInfo" :formBase="formBase" @close="closeFn"/>
  <pageTool  @pageChange="pageChange" @pageSizeChange="pageSizeChange" :total="total" :paginationPage="paginationPage" :paginationPagesize="paginationPagesize" />
  </div>
</template>

<script>
import { provinces, citys } from '@/api/hmmm/citys.js'
import companysAdd from '../components/companys-add.vue'
import { list, disabled, remove } from '@/api/hmmm/companys'
import pageTool from '../components/page-tool.vue'
export default {
  components: {
    companysAdd,
    pageTool
  },
  data () {
    return {
      total: '',
      paginationPage: 1,
      paginationPagesize: 10,
      dialogVisible: false,
      formBase: {
        tags: '',
        province: '',
        city: '',
        shortName: '',
        company: '',
        isFamous: false,
        remarks: ''
      },
      titleInfo: {
        text: '',
        pageTitle: '用户'
      },
      tableData: [],
      formInline: {
        id: '',
        number: '',
        shortName: '',
        tags: '',
        creatorID: '',
        addDate: '',
        remarks: '',
        state: ''
      },
      fromDate: {
        tags: '',
        province: '',
        city: '',
        shortName: '',
        state: ''
      },
      options: [],
      option: [],
      optionS: [
        {
          label: '启用',
          value: '1'
        },
        {
          label: '禁用',
          value: '0'
        }
      ],
      pageSize: {
        pagesize: 10,
        page: 1
      }
    }
  },
  created () {
    // console.log(provinces())
    this.getList()
    // console.log(citys())
    this.options = provinces()
  },
  methods: {
    closeFn () {
      this.formBase = {
        tags: '',
        province: '',
        city: '',
        shortName: '',
        company: '',
        isFamous: false,
        remarks: ''
      }
    },
    newDataes () {
      console.log(1)
      this.getList()
    },
    pageSizeChange (pageSize) {
      // console.log(pageSize)
      this.pageSize.pagesize = pageSize
      this.getList()
    },
    // 每页显示页数
    pageChange (pageNum) {
      // console.log(pageNum)
      this.pageSize.page = pageNum
      this.getList()
    },
    // 删除
    delFn (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(row)
        this.getList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 禁用
    async  uploadList (row) {
      this.$confirm(`已启用${this.tableData.state === 1 ? '启用' : '禁用'}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // console.log(data)
        await disabled({ id: row.id, state: row.state === 1 ? 0 : 1 })
        this.getList()
        this.$message({
          type: 'success',
          message: '操作成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    // 编辑
    async editList (id) {
      // console.log(id)
      this.titleInfo.text = '编辑'
      this.$refs.dataForm.dialogFormV()
      const {
        data: { items }
      } = await list()
      console.log(items)
      items.forEach((item) => {
        if (item.id === id) {
          this.formBase = item
        }
      })
      this.formBase.isFamous = this.formBase.isFamous !== 1
      // console.log(num)
    },
    // 新增学科
    newSubject () {
      this.titleInfo.text = '新增'
      this.$refs.dataForm.dialogFormV()
    },
    // 查找
    listFind () {
      this.changeList()
    },
    clear () {
      this.fromDate = {
        tags: '',
        province: '',
        city: '',
        shortName: '',
        state: ''
      }
    },
    changeFn () {
      // console.log(citys(this.value))
      this.option = citys(this.fromDate.province)
      this.fromDate.city = this.option[0]
    },
    async getList () {
      const {

        data: { counts, items }
      } = await list(this.pageSize)
      console.log(items)
      this.tableData = items
      this.total = counts
    },
    changeWhat (value) {
      // console.log(value)
      this.fromDate.state = value
    },
    async changeList () {
      const {
        data: { items }
      } = await list(this.fromDate)
      // console.log(items)
      this.tableData = items
      this.clear()
    }
  }
}
</script>

<style scoped lang="less">
.el-input__inner {
  height: 28px;
  width: 326px;
}

.buttom {
  margin-left: 75px;
  .clearTwo {
    margin-left: 40px;
    .el-button {
      width: 56px;
      height: 32px;
      // text-align: center !important;
      padding-right: 10px;
      font-size: 12px;
      padding-right: 40px;
    }
  }
}
.up {
  display: flex;
  justify-content: space-evenly;
}
.body {
  box-sizing: border-box;
  padding: 20px;
  max-width: 100%;
  margin: 10px;
  background-color: #fff;
  color: #303133;
  .alert {
    height: 31px;
    width: 100%;
    padding-left: 16px;
    margin: 15px 0;
    border-radius: 3px;
    background-color: #f4f4f4;
    line-height: 35px;
    color: #909399;
    font-size: 13px;
  }
}
</style>
